/**
 * @export
 * @class Calendar 生成日历数据
 * @author gary
 * @param {Number} start 开始年
 * @param {Number} end 结束年
 * @return {Array} [{year:Number,months:Array,days:Array}]
 */
export class Calendar {
    constructor(start, end) {
        return this.generateCalendar(start, end);
    }

    is_leap(year) {
        return (year%100 == 0? (year%400==0?1:0) : (year%4==0?1:0));
    }

    generateTemplate(year) {
        let array = new Array(31, 28+this.is_leap(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        let days = [];
        for ( let i = 0; i <= array.length; i++ ) {
            let day = [];
            for ( let x = 0; x < array[i]; x++ ) {
                day.push(x+1);
            }
            days.push(day)
        }
        return {
            year: year,
            months: [1,2,3,4,5,6,7,8,9,10,11,12],
            days: days
        }
    }

    generateCalendar(start, end) {
        var calendar = [];
        for ( let i = start; i < end; i++ ) {
            calendar.push(this.generateTemplate(i));
        }
        return calendar;
    }
}

/**
 * 映射对象键值对生成新的对象
 * @param {Object} source 
 * @param {Oject} dest 
 * @returns 
 */
export function modifyObjectKey(source, dest) {
    if(typeof source !== 'object' && typeof dest !=='object') {throw '传入参数必须为对象'};
    var obj = {};
    for( let i in source ) {
        if (dest[i]) {
            if (Array.isArray(source[i])) {
                obj[i] = modifyArrayObjectKey(source[i], dest[i]);
            }else{
                obj[dest[i]] = source[i];
            }
        }
    }
    return obj;
}
/**
 * 映射数组对象键值对生成新的对象（批量）
 * @export
 * @param {Array} source 
 * @param {Object} dest 键值对方式 键为需要更改的键  值为新对象的键
 * @returns 
 */
export function modifyArrayObjectKey(source, dest) {
    var obj = [];
    for (let i=0; i<source.length; i++) {
        obj.push(modifyObjectKey(source[i], dest));
    }
    return obj;
}


/**
 * 批量设置cookie
 * @param arr [[name, value],[name, value]] 
 */
export const setCookie = (arr) => {
    let hours = 2;
    var exp = new Date();
    exp.setTime(exp.getTime() + hours*60*60*1000);
    arr.map((item) => {
        document.cookie = item[0]+'='+escape(item[1])+';expires='+exp.toGMTString();
    })
};

/**
 * 获取某个cookie值
 * @param {*string} name 
 */
export const getCookie = (name) => {
    var cookies = document.cookie.split(';');
    for ( let i = 0; i < cookies.length; i++ ) {
        var temp = cookies[i].split("=");
        if ( temp[0].trim() == name ) {
            return unescape(temp[1])
        }else{
            return '';
        }
    }
}

/**
 * 删除全部cookie
 */
export const delCookieAll = () => {
    var cookies = document.cookie.split(';');
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    for ( let i = 0; i < cookies.length; i++ ) {
        document.cookie = cookies[i]+';expires='+exp.toGMTString();
    }
}

export const decodeHtml = function(s) {
    var HTML_DECODE = {
        "&lt;": "<",
        "&gt;": ">",
        "&amp;": "&",
        "&nbsp;": " ",
        "&quot;": "\"",
        "&copy;": ""

        // Add more
    };

    var REGX_HTML_ENCODE = /"|&|'|<|>|[\x00-\x20]|[\x7F-\xFF]|[\u0100-\u2700]/g;

    var REGX_HTML_DECODE = /&\w+;|&#(\d+);/g;

    var REGX_TRIM = /(^\s*)|(\s*$)/g;

    s = (s != undefined) ? s : "";
    return (typeof s != "string") ? s :
        s.replace(REGX_HTML_DECODE,
            function ($0, $1) {
                var c = HTML_DECODE[$0];
                if (c == undefined) {
                    // Maybe is Entity Number
                    if (!isNaN($1)) {
                        c = String.fromCharCode(($1 == 160) ? 32 : $1);
                    } else {
                        c = $0;
                    }
                }
                return c;
            });
};
